<?php

namespace addons\workorder\model;

use think\Model;
use traits\model\SoftDelete;

/**
 * 工程师模型
 */
class Engineer extends Model
{
    use SoftDelete;

    // 表名
    protected $name = 'workorder_engineers';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected        $createTime             = 'createtime';
    protected        $updateTime             = false;
    protected        $deleteTime             = 'deletetime';
    protected static $showEngineerInfoConfig = [];

    protected $append = [
        'all_order_number'
    ];

    protected static function init()
    {
        $workorderConfig              = get_addon_config('workorder');
        self::$showEngineerInfoConfig = explode(',', $workorderConfig['show_engineer_info']);
    }

    public function getAllOrderNumberAttr($value, $data)
    {
        if (!in_array('all_order_number', self::$showEngineerInfoConfig)) {
            return false;
        }

        return Orders::where(['engineer_id' => $data['id']])->count('id');
    }

    public static function getEngineerInfoConfig()
    {
        return self::$showEngineerInfoConfig;
    }

    public function user()
    {
        return $this->hasOne('app\admin\model\User', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
    }
}